export class LocalStorageDao {
    static current = new LocalStorageDao();
    storagePrefix = ""; //kds_
    get(key) {
        return JSON.parse(localStorage.getItem(this.storagePrefix + key));
    }

    add(key, obj) {
        // 如果属性存在，不报错，不覆盖已有值，直接返回
        if (localStorage.getItem(this.storagePrefix + key)) {
            return localStorage.getItem(this.storagePrefix + key);
        } else {
            localStorage.setItem(this.storagePrefix + key, obj);
            return obj;
        }
    }

    put(key, obj) {
        // 如果key存在，直接更新，如果不存在，新建
        try {
            localStorage.setItem(this.storagePrefix + key, JSON.stringify(obj));
            return obj;
        } catch (e) {
            return e;
        }
    }

    remove(key) {
        var obj = localStorage.getItem(this.storagePrefix + key);
        localStorage.removeItem(this.storagePrefix + key);
        return obj;
    }
}